<?php
namespace app\manage\controller;

use think\Db;
class Game extends Base
{
    //壁纸列表
    public function wallpaperList() {
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $search = input('post.search');
        $where = [
            ['del','=',0]
        ];
        if($search) {
            $where[] = ['title','like',"%{$search}%"];
        }
        try {
            $count = Db::table('mp_wallpaper')->where($where)->count();
            $list = Db::table('mp_wallpaper')->where($where)->limit(($curr_page - 1)*$perpage,$perpage)->select();
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }
    //添加壁纸
    public function wallpaperAdd() {
        $post['title'] = input('post.title');
        checkPost($post);
        $post['desc'] = input('post.desc');
        $tmp_pic = input('post.pic','');
        $post['create_time'] = time();
        try {
            if(!$tmp_pic) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_pic);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_pic,'upload/wallpaper/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_wallpaper')->insert($post);
        }catch (\Exception $e) {
            if(isset($post['pic'])) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
    //壁纸详情
    public function wallpaperDetail() {
        $post['id'] = input('post.id','');
        try {
            $whereitem = [
                ['id','=',$post['id']]
            ];
            $info = Db::table('mp_wallpaper')->where($whereitem)->find();
            if(!$info) {
                return ajax('非法参数',-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($info);
    }
    //修改壁纸POST
    public function wallpaperMod() {
        $post['title'] = input('post.title');
        $post['id'] = input('post.id');
        checkPost($post);
        $post['desc'] = input('post.desc');
        $tmp_pic = input('post.pic','');
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $paper_exist = Db::table('mp_wallpaper')->where($where)->find();
            if(!$paper_exist) {
                return ajax('非法参数',-4);
            }
            $old_pic = $paper_exist['pic'];
            if(!$tmp_pic) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_pic);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_pic,'upload/wallpaper/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_wallpaper')->where($where)->update($post);
        }catch (\Exception $e) {
            if(isset($post['pic']) && $post['pic'] !== $old_pic) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        if(isset($post['pic']) && $post['pic'] !== $old_pic) {
            $this->deleteObject($old_pic);
        }
        return ajax();
    }
    //删除壁纸
    public function wallpaperDelete() {
        $post['id'] = input('post.id');
        try {
            $whereitem = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_wallpaper')->where($whereitem)->find();
            if(!$exist) {
                return ajax('非法参数',-1);
            }
            Db::table('mp_wallpaper')->where($whereitem)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
//        $this->deleteObject($exist['pic']);
        return ajax();
    }

}